
Imports std
Imports System.IO

Module main

    Public Sub prepareSelectColumn(ByRef input As String, ByRef indexColumn As vector(Of Int32))
        Dim i As Int32
        Dim parts As vector(Of String) = Nothing
        If indexColumn Is Nothing Then
            indexColumn = New vector(Of Int32)
        Else
            indexColumn.clear()
        End If
        strsepar(input, character.comma + character.semicolon + character.sheffer + character.tab + character.blank _
                        + character.fullStop + character.leftSlash + character.rightSlash, parts)
        For i = 0 To parts.size() - 1
            If Not isemptystring(parts(i)) Then
                indexColumn.push_back(Convert.ToInt32(parts(i)))
            End If
        Next
    End Sub

    Public Sub prepareSelectRow(ByRef input As String, ByRef from As Int32, ByRef [to] As Int32)
        Dim temp As vector(Of Int32) = Nothing
        prepareSelectColumn(input, temp)
        from = 0
        [to] = -1
        If temp.size > 0 Then
            from = temp(0)
            If temp.size > 1 Then
                [to] = temp(1)
            End If
        End If
    End Sub

    Sub Main(ByVal args() As String)
        On Error Resume Next
        Dim istream As FileStream = Nothing
        Dim reader As TextReader = Nothing
        Dim ostream As FileStream = Nothing
        Dim writer As TextWriter = Nothing
        Dim input As String = Nothing
        Dim parts As vector(Of String) = Nothing
        Dim selectColumn As vector(Of Int32) = Nothing
        Dim rowfrom As Int32
        Dim rowto As Int32
        Dim i As Int32
        Dim j As Int32

        If args.Length < 2 Or args.Length Mod 2 <> 0 Or args.Length > 6 Then
            raiseError("argument count error, select [-r rowfrom,rowto] [-c column1,column2,...] input output, " + _
                        "if input or output is /-, use console, row# starts from 0.", errorHandle.errorType.critical)
        Else
            If args(args.Length() - 1) = "/-" Then
                writer = Console.Out
            Else
                ostream = New FileStream(args(args.Length() - 1), FileMode.Create, FileAccess.Write, FileShare.Read)
                writer = New StreamWriter(ostream)
            End If
            assert(Not writer Is Nothing, "file no found, " + args(args.Length() - 1))

            If args(args.Length() - 2) = "/-" Then
                reader = Console.In
            Else
                istream = New FileStream(args(args.Length() - 2), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
                reader = New StreamReader(istream)
            End If
            assert(Not reader Is Nothing, "file no found, " + args(args.Length() - 2))

            prepareSelectRow("", rowfrom, rowto)
            For i = 0 To args.Length - 3 Step 2
                If strsame(args(i), "-r", False) Then
                    prepareSelectRow(args(i + 1), rowfrom, rowto)
                ElseIf strsame(args(i), "-c", False) Then
                    prepareSelectColumn(args(i + 1), selectColumn)
                End If
            Next

            For i = 0 To rowfrom - 1
                reader.ReadLine()
            Next
            input = reader.ReadLine()
            While (i < rowto OrElse rowto = -1) AndAlso (Not input Is Nothing)
                kickNewlineChar(input)
                If Not selectColumn Is Nothing Then
                    strsepar(input, character.tab, parts)
                    For j = 0 To selectColumn.size() - 1
                        If j > 0 Then
                            writer.Write(character.tab)
                        End If
                        If parts.size >= selectColumn(j) AndAlso selectColumn(j) >= 0 Then
                            writer.Write(Convert.ToString(parts(selectColumn(j))))
                        End If
                    Next
                    writer.WriteLine()
                Else
                    writer.WriteLine(input)
                End If
                i += 1
                input = reader.ReadLine()
            End While

            closestream(reader)
            closestream(writer)
        End If
    End Sub

End Module
